#next
Description: Get the next item from an iterator (by calling the object's __next__
method).
def next(iterator):
'''
Get the next item from an iterator,
raises StopIteration if there is no next item
:param iterator: An iterator
:return: The next item of the iterator
'''
def next(iterator, default):
'''
Get the next item from an iterator, return default if there is no next item
:param iterator: An iterator
:param default: Default value
:return: The next item of the iterator
'''
Example:
import io
# Iterator
class Iterator:
def __init__(self, stop):
self.__stop = stop
self.__current = 0
def __next__(self):
if self.__current < self.__stop:
self.__current += 1
return self.__current - 1
else:
raise StopIteration
# Usage
iterator = Iterator(10)
while (value := next(iterator, None)) is not None:
print(value)